<?php

namespace app\index\service\wrappers;

use app\common\model\ConsultingMessage;
use app\index\dao\ConsultingCommentDao;

/**
 *
 * Class CommentMessageWrapper
 * @package app\consulting\service\wrappers
 */
class CommentMessageWrapper implements MessageWrapperInterface
{

    private ConsultingCommentDao $consultingCommentDao;

    public function __construct(ConsultingCommentDao $consultingCommentDao)
    {

        $this->consultingCommentDao = $consultingCommentDao;
    }

    /**
     * 包装动态消息
     * @param ConsultingMessage $message
     * @return void
     */
    public function wrapMessage(ConsultingMessage $message)
    {
        // 查询并获取 server 内容
        $id = $message->id;
        if (is_id($id)) {
            $server = $this->consultingCommentDao->findCardMessage($id);
            if ($server) {
                $message->set('content', $server);
            }
        }
    }

    /**
     * 批量包装动态消息
     * @param array $messages
     * @return void
     */
    public function wrapMessages(array $messages)
    {
        $ids = [];
        foreach ($messages as $message) {
            $id = $message->id;
            if (is_id($id)) {
                $ids[] = $id;
            }
        }

        $ids = array_values(array_unique($ids));

        if (!empty($ids)) {
            $comments = $this->consultingCommentDao->selectCardMessage($ids)->column(null, 'consultingMessageId');
            foreach ($messages as $message) {
                $id = $message->id;
                $comment = $comments[$id] ?? null;
                if ($comment) {
                    $message->set('content', $comment);
                }
            }
        }
    }
}
